<div class="layui-fluid">
    <div class="layui-card">
        <script template type="text/html" lay-url="/Tool/getTableConfig" lay-type="post" lay-data="{TableNo:'{{layui.admin.getPageSign()||layui.router().search.TableNo}}'}" lay-done="layui.data.doneList(d)">
            <div class="layui-card-body">
                <table id="{{layui.admin.getPageSign()||layui.router().search.TableNo}}-table" lay-filter="{{layui.admin.getPageSign()||layui.router().search.TableNo}}-table"></table>
            </div>
        </script>
    </div>
</div>

<script>
    layui.data.doneList = function (res) {
        if (res.data.tempTable) {
            var tableId = layui.admin.getPageSign() || layui.router().search.TableNo
                , searchFormNo = tableId + '-search-form'
                , subFormNo = tableId + '-form';;
            
            var options = layui.admin.parse(res.data.tempTable);

            if (options.options)
                delete options.options;

            options.buildType = 1;
            options.tableId = tableId;

            if (options.tableOptions) {
                if (options.tableOptions.options)
                    delete options.tableOptions.options;

                if (options.tableOptions.where) {
                    options.tableOptions.where = layui.admin.funObj(options.tableOptions.where);
                    if (options.tableOptions.where.SearchWhere)
                        options.tableOptions.where.SearchWhere = JSON.stringify(options.tableOptions.where.SearchWhere);
                }
                
                if (options.tableOptions.initSort)
                    options.tableOptions.initSort = layui.admin.funObj(options.tableOptions.initSort);
            }

            if (options.formOptions) {
                if (options.formOptions.options)
                    delete options.formOptions.options;

                if (!options.formOptions.url && !options.formOptions.formLay && res.data.subForm) {
                    var subFormItemsData = layui.admin.parse(res.data.subForm);
                    var html = '<form class="layui-form " lay-filter="' + subFormNo + '" id="' + subFormNo + '">'
                        + layui.admin.getFormHtml(subFormItemsData)
                        + layui.admin.getFormBtn(tableId)
                        + '</form>';
                    options.formOptions.formLay = subFormNo;
                    options.formOptions.formHtml = html;
                    options.formOptions.FormItemsData = subFormItemsData;
                }
            }
            
            var searchFormItemsData, html = '';
            if (res.data.searchForm) {
                searchFormItemsData = layui.admin.parse(res.data.searchForm);
                html = '<form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="' + searchFormNo + '" id="' + searchFormNo + '">'
                    + layui.admin.getFormHtml(searchFormItemsData)
                    + '</form>';
                layui.$('#' + tableId + '-table').parent().before(html);
            }

            if (options.tableType === 'table') {
                layui.use('reTable', function () {
                    var $ = layui.$
                        , reTable = layui.reTable
                        , reForm = layui.reForm
                        , form = layui.form;

                    if (res.data.searchForm) {

                        $.each(searchFormItemsData, function (idx, json) {
                            reForm.bindFormHtml(json, null, searchFormNo);
                        });

                        form.render(null, searchFormNo);
                    }

                    reTable.initTable(options);
                })
            } else {
                layui.use('reTreeGrid', function () {
                    var $ = layui.$
                        , reTreeGrid = layui.reTreeGrid
                        , reForm = layui.reForm
                        , form = layui.form;

                    if (res.data.searchForm) {

                        $.each(searchFormItemsData, function (idx, json) {
                            reForm.bindFormHtml(json, null, searchFormNo);
                        });

                        form.render(null, searchFormNo);
                    }

                    reTreeGrid.initTreeGrid(options);
                })
            }
        }
    }
</script>